package com.excellent.mvvm.widget

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import com.excellent.mvvm.R

class ConfirmDialogFragment : DialogFragment() {

    private var message: Int = 0
    private var leftText: Int = 0
    private var rightText: Int = 0

    private var onLeftClick: (() -> Unit)? = null
    private var onRightClick: (() -> Unit)? = null

    fun setMessage(message: Int) = apply { this.message = message }
    fun setLeft(leftText: Int, listener: () -> Unit) = apply {
        this.leftText = leftText
        this.onLeftClick = listener
    }

    fun setRight(rightText: Int, listener: () -> Unit) = apply {
        this.rightText = rightText
        this.onRightClick = listener
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        dialog?.window?.setWindowAnimations(R.style.DialogAnimation_scale)
        val view = inflater.inflate(R.layout.dialog_confirm_layout, container, false)

        val tvMessage = view.findViewById<TextView>(R.id.tv_message)
        val btnLeft = view.findViewById<Button>(R.id.btn_left)
        val btnRight = view.findViewById<Button>(R.id.btn_right)

        tvMessage.setText(message)
        btnLeft.setText(leftText)
        btnRight.setText(rightText)

        btnLeft.setOnClickListener {
            onLeftClick?.invoke()
            dismiss()
        }

        btnRight.setOnClickListener {
            onRightClick?.invoke()
            dismiss()
        }

        //设置弹窗背景透明，避免默认 padding
        dialog?.window?.setBackgroundDrawableResource(android.R.color.transparent)

        return view
    }
}